package com.loongtech.bi.action.advertisment;

import com.loongtech.bi.advertisment.controller.BaseAdvAction;
import com.loongtech.bi.advertisment.model.ChannelRtData;
import com.loongtech.bi.advertisment.model.PayHourData;
import com.loongtech.bi.advertisment.model.PayMinData;
import com.loongtech.bi.advertisment.model.chart.AdvChartUIData1;
import com.loongtech.bi.advertisment.model.chart.AdvChartUIData2;
import com.loongtech.bi.advertisment.model.chart.AdvChartUIData3;
import com.loongtech.bi.advertisment.model.chart.AdvChartUIData4;
import com.loongtech.bi.advertisment.service.PayUserService;
import com.loongtech.bi.manager.system.SysUserManager;
import com.loongtech.bi.support.chart.ChartTool;
import com.loongtech.bi.support.chart.ChartType;
import com.loongtech.bi.support.chart.ChartUIData;
import com.loongtech.core.util.Time;
import com.loongtech.core.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * ClassName: ChannelDataAction
 * 
 * @Description: 广告数据分析 - 实时：渠道数据
 * @author liuchen
 * @date 2016-11-30 下午2:18:00
 */
@Controller
@Scope("prototype")
@RequestMapping("/market/channelDataMarket")
public class RTChannelDataBiAction extends BaseAdvAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private List<PayHourData> payHourDatas;

	private List<ChannelRtData> channelDatas;

	private List<PayMinData> payMinDatas;

	@Resource(name = "payUserService")
	private PayUserService payUserService;

    @Autowired
    private SysUserManager sysUserManager;

	@RequestMapping(value = "initParam.do", method = { RequestMethod.GET, RequestMethod.POST })
	public void initParam(HttpServletRequest request, HttpServletResponse response, @RequestParam String uid, String callback) {
		try {
            Map<String, Object> result = new HashMap<String, Object>();
            Integer userid = sysUserManager.getUserIdByName(uid);
            if (null == userid) {
                result.put("res", 1);
                result.put("msg", "帐户错误，请联系管理员!");
                printToJson(response, result, callback);
                return;
            }
            initData(userid);

			result.put("res", "0");
			result.put("affiliation", getAffiliation());
			result.put("channelName", getChannelName());
			result.put("agent", getAgent());
			result.put("platform", getPlatform());
			result.put("paytype", getPaytype());
			result.put("advPosition", getAdvPosition());
			printToJson(response, result, callback);
		} catch (Exception e) {
			e.printStackTrace();
			Map<String, Object> result = new HashMap<String, Object>();
			result.put("res", "1");
			result.put("msg", e.getMessage());
			printToJson(response, result, callback);
			return;
		}
	}

	@RequestMapping(value = "search.do", method = { RequestMethod.GET, RequestMethod.POST })
	public void search(HttpServletRequest request, HttpServletResponse response, @RequestParam String day,
			@RequestParam String agent, @RequestParam String platform, @RequestParam String affiliation,
			@RequestParam String channelName, @RequestParam String paytype, @RequestParam String advPosition,@RequestParam String uid,
                       String callback) {
		try {
            Map<String, Object> result = new HashMap<String, Object>();
            Integer userid = sysUserManager.getUserIdByName(uid);
            if (null == userid) {
                result.put("res", 1);
                result.put("msg", "帐户错误，请联系管理员!");
                printToJson(response, result, callback);
                return;
            }
            initData(userid);

			if (hasAgents == null || hasAgents.size() <= 0) {
				result.put("res", "1");
				result.put("msg", "此用户无任何代理商权限，请联系管理员配置!");
				printToJson(response, result, callback);
				return;
			}

            String[] affiliations = "".equals(affiliation) ? getAffiliation().toArray(new String[getAffiliation().size()]) : Utils.matchesString(affiliation).split(",");
            String[] channelNames = "".equals(channelName) ? getChannelName().toArray(new String[getChannelName().size()]) : Utils.matchesString(channelName).split(",");
            String[] agents = "".equals(agent) ? getAgent().toArray(new String[getAgent().size()]) : Utils.matchesString(agent).split(",");
            String[] platforms = "".equals(platform) ? getPlatform().toArray(new String[getPlatform().size()]) : Utils.matchesString(platform).split(",");
            String[] paytypes = "".equals(paytype) ? getPaytype().toArray(new String[getPaytype().size()]) : Utils.matchesString(paytype).split(",");
            String[] advPositions = "".equals(advPosition) ? getAdvPosition().toArray(new String[getAdvPosition().size()]) : Utils.matchesString(advPosition).split(",");

            payHourDatas = new ArrayList<>();
			channelDatas = new ArrayList<>();
            ChartUIData lineChart1 = new ChartUIData();
            ChartUIData lineChart2 = new ChartUIData();
            ChartUIData lineChart3 = new ChartUIData();
            ChartUIData lineChart4 = new ChartUIData();

			payHourDatas = payUserService.getListHourData(day, agents, platforms);
			channelDatas = payUserService.getListChannelData(day, affiliations, channelNames, agents, platforms,
					paytypes, advPositions);

            String lastday = Time.getDateByHNum(Time.formatDate(day,"yyyy-MM-dd"), -1);

            List<AdvChartUIData1> data1 = payUserService.getAdvActDate(day, lastday, selectedAgent, selectedPlatform);
            if (data1 != null && data1.size() > 0) {
                lineChart1.setJsId("1");
                lineChart1.setJsonType(ChartType.SPLINE.type);
                lineChart1
                        .setJsonStr(new ChartTool(data1, AdvChartUIData1.class, "").loadData(lineChart1.getJsonType()));
            }
            List<AdvChartUIData2> data2 = payUserService.getAdvActMacDate(day, lastday, selectedAgent, selectedPlatform);
            if (data2 != null && data2.size() > 0) {
                lineChart2.setJsId("2");
                lineChart2.setJsonType(ChartType.SPLINE.type);
                lineChart2
                        .setJsonStr(new ChartTool(data2, AdvChartUIData2.class, "").loadData(lineChart2.getJsonType()));
            }
            List<AdvChartUIData3> data3 = payUserService.getAdvActUserDate(day, lastday, selectedAgent, selectedPlatform);
            if (data3 != null && data3.size() > 0) {
                lineChart3.setJsId("3");
                lineChart3.setJsonType(ChartType.SPLINE.type);
                lineChart3
                        .setJsonStr(new ChartTool(data3, AdvChartUIData3.class, "").loadData(lineChart3.getJsonType()));
            }
            List<AdvChartUIData4> data4 = payUserService.getAdvActPayDate(day, lastday, selectedAgent, selectedPlatform);
            if (data4 != null && data4.size() > 0) {
                lineChart4.setJsId("4");
                lineChart4.setJsonType(ChartType.SPLINE.type);
                lineChart4
                        .setJsonStr(new ChartTool(data4, AdvChartUIData4.class, "").loadData(lineChart4.getJsonType()));
            }

			result.put("res", "0");
			result.put("payHourDatas", payHourDatas);
			result.put("channelDatas", channelDatas);

            result.put("activeChart", lineChart1);
            result.put("newActChart", lineChart2);
            result.put("newAccountChart", lineChart3);
            result.put("newPayChart", lineChart4);
			printToJson(response, result, callback);
		} catch (Exception e) {
			e.printStackTrace();
			Map<String, Object> result = new HashMap<String, Object>();
			result.put("res", "1");
			result.put("msg", e.getMessage());
			printToJson(response, result, callback);
			return;
		}
	}

	@RequestMapping(value = "searchChannelDetail.do", method = { RequestMethod.GET, RequestMethod.POST })
	public void searchChannelDetail(HttpServletRequest request, HttpServletResponse response, @RequestParam String day,
			@RequestParam String agent, @RequestParam String channelName, @RequestParam String uid, String callback) {
		try {
            Map<String, Object> result = new HashMap<String, Object>();
            Integer userid = sysUserManager.getUserIdByName(uid);
            if (null == userid) {
                result.put("res", 1);
                result.put("msg", "帐户错误，请联系管理员!");
                printToJson(response, result, callback);
                return;
            }
            initData(userid);

            agent = Utils.matchesString(agent);
            channelName = Utils.matchesString(channelName);

			if (hasAgents == null || hasAgents.size() <= 0) {
				result.put("res", "1");
				result.put("msg", "此用户无任何代理商权限，请联系管理员配置!");
				printToJson(response, result, callback);
				return;
			}

			payMinDatas = payUserService.getChannelDetailData(day, channelName, agent);

			result.put("res", "0");
			result.put("payMinDatas", payMinDatas);
			printToJson(response, result, callback);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@RequestMapping(value = "searchChannelData.do", method = { RequestMethod.GET, RequestMethod.POST })
	public void searchChannelData(HttpServletRequest request, HttpServletResponse response, @RequestParam String day,
			@RequestParam String agent, @RequestParam String platform, @RequestParam String affiliation,
			@RequestParam String channelName, @RequestParam String paytype, @RequestParam String advPosition,@RequestParam String uid,
                                  String callback) {
		try {
            Map<String, Object> result = new HashMap<String, Object>();
            Integer userid = sysUserManager.getUserIdByName(uid);
            if (null == userid) {
                result.put("res", 1);
                result.put("msg", "帐户错误，请联系管理员!");
                printToJson(response, result, callback);
                return;
            }
            initData(userid);

			if (hasAgents == null || hasAgents.size() <= 0) {
				result.put("res", "1");
				result.put("msg", "此用户无任何代理商权限，请联系管理员配置!");
				printToJson(response, result, callback);
				return;
			}

            String[] affiliations = "".equals(affiliation) ? getAffiliation().toArray(new String[getAffiliation().size()]) : Utils.matchesString(affiliation).split(",");
            String[] channelNames = "".equals(channelName) ? getChannelName().toArray(new String[getChannelName().size()]) : Utils.matchesString(channelName).split(",");
            String[] agents = "".equals(agent) ? getAgent().toArray(new String[getAgent().size()]) : Utils.matchesString(agent).split(",");
            String[] platforms = "".equals(platform) ? getPlatform().toArray(new String[getPlatform().size()]) : Utils.matchesString(platform).split(",");
            String[] paytypes = "".equals(paytype) ? getPaytype().toArray(new String[getPaytype().size()]) : Utils.matchesString(paytype).split(",");
            String[] advPositions = "".equals(advPosition) ? getAdvPosition().toArray(new String[getAdvPosition().size()]) : Utils.matchesString(advPosition).split(",");

			channelDatas = new ArrayList<>();

			channelDatas = payUserService.getListChannelData(day, affiliations, channelNames, agents, platforms,
					paytypes, advPositions);

			result.put("res", "0");
			result.put("channelDatas", channelDatas);
			printToJson(response, result, callback);

		} catch (Exception e) {
			e.printStackTrace();
			Map<String, Object> result = new HashMap<String, Object>();
			result.put("res", "1");
			result.put("msg", e.getMessage());
			printToJson(response, result, callback);
			e.printStackTrace();
		}
	}
}
